package be.fedict.eid.diagnostics.tests;

import be.fedict.eid.diagnostics.DiagnosticTest;
import be.fedict.eid.diagnostics.TestFailedException;
import be.fedict.eid.diagnostics.annotations.TestName;
import be.fedict.eid.diagnostics.helper.DataTreeBuilder;
import be.fedict.eid.diagnostics.system.OSException;
import be.fedict.eid.diagnostics.system.OperatingSystem;
import be.fedict.eid.diagnostics.system.data.SystemService;

@TestName("Smart Card Service State Test")
public class SmartCardServiceStatusTest extends DiagnosticTest {

	
	protected boolean runTest() throws TestFailedException {
		
		OperatingSystem os;
		try {
			os = new OperatingSystem();
		} catch (OSException e) {
			throw new TestFailedException("Could not establish type of Operating System.");
		}
		
		SystemService smartCardService = os.getServiceManager().getService("SCardSvr");
		
		if (smartCardService == null) {
			throw new TestFailedException("Could not find the Smart Card Service (SCardSvr).");
		} else {
			if (smartCardService.getStatus().equals(SystemService.STATE.RUNNING)) {
				rootNode.add(DataTreeBuilder.buildNode("state", smartCardService.getStatus().toString()));
									
				return true;
			}
			else
				throw new TestFailedException("Smart Card Service not running, current status = " + smartCardService.getStatus());
		}
		
	}

	protected String getRootName() {
		return "scardsvr";
	}

}
